<?php
/**
 * Created by PhpStorm.
 * User: zhengkunming
 * Date: 2018/12/26
 * Time: 4:24 PM
 */

require_once 'mysql_config.php';

$openid = isset($_POST['openid']) ? $_POST['openid'] : '';
$shop = isset($_POST['shop']) ? $_POST['shop'] : '';

$check_in_score = 6;


if ('' === $openid) {
    $data = array(
        'code' => -1,
        'data' => array(),
        'msg' => 'require openid'
    );
    rtJson($data);
    return;
}


$query = 'SELECT * FROM check_in WHERE openid = "' . $openid . '" AND created = "' . $current_date . '"';

$query_res = mysqli_query($link, $query);

if (FALSE === $query_res) {
    $data = array(
        'code' => -4,
        'data' => array(),
        'msg' => 'database err'
    );
    rtJson($data);
    return;
}

$query_num_rows = $query_res->num_rows;

if (0 === $query_num_rows) {
    $query = 'INSERT INTO check_in (openid, created, score ,shop) VALUE ("' . $openid . '","' . $current_date . '",' . $check_in_score . ',"'.$shop.'")';

//    $query_users = 'UPDATE users SET score = score + ' . $check_in_score . ' WHERE openid = "' . $openid . '"';

    $query_res = mysqli_query($link, $query);

//    $query_users_res = mysqli_query($link, $query_users);

    if (TRUE === $query_res) {
        $data = array(
            'code' => 0,
            'data' => array(),
            'msg' => 'update success'
        );
        rtJson($data);
        return;
    } else {
        $data = array(
            'code' => -3,
            'data' => array(),
            'msg' => 'update fail'
        );
        rtJson($data);
        return;
    }
} else {
    $check_in = $query_res->fetch_object();
    $data = array(
        'code' => -2,
        'data' => $check_in,
        'msg' => 'have check in'
    );
    rtJson($data);
    return;
}
